using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using OpenComposite.Repository.Properties;
using OpenComposite.EII.Designer.SCA;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class UCService : RepositoryItemUI
	{

		#region Initialization

		public UCService(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{
			InitializeComponent();

			// 
			// ucRepositoryItemListEx1
			// 
			this.ucRepositoryItemListEx1 = new OpenComposite.EII.RepositoryUIs.UCRepositoryItemListEx(this.serviceProvider);
			this.ucRepositoryItemListEx1.Location = new System.Drawing.Point(6, 137);
			this.ucRepositoryItemListEx1.Dock = DockStyle.Fill;
			this.ucRepositoryItemListEx1.Name = "ucRepositoryItemListEx1";
			this.ucRepositoryItemListEx1.RepositoryItem = null;
			this.ucRepositoryItemListEx1.ShowFoldersInTabs = true;
			this.ucRepositoryItemListEx1.Size = new System.Drawing.Size(384, 110);
			this.ucRepositoryItemListEx1.TabIndex = 5;
			this.pnlSubItems.Controls.Add(this.ucRepositoryItemListEx1);
		}

		#endregion

		#region Public Members

		#region Properties
		public override IRepositoryItem RepositoryItem
		{
			get
			{
				save();
				return _service;
			}
			set
			{
				base.RepositoryItem = value;
				if ( value is Service ) {
					_service = (Service)value;
					load();
				}
			}
		}

		public override string TypeText
		{
			get { return Resources.sService; }
		}

		public override string TypeDescription
		{
			get { return ""; }
		}
		#endregion

		#region Events
		#endregion

		#region Methods
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private Service _service = null;
		//private DataTable _dtChildServices = null;
		#endregion

		#region Methods
		private void load()
		{
			this.SuspendLayout();
			if ( _service == null )
				return;

			txtServiceNm.Text = _service.Name;
			txtServiceDesc.Text = _service.Description;

			if ( _service.Type == Service.ServiceType.UserInterface ) {
				pnlUI.Visible = true;
				UIComponentProperties uiprops = getUIProperties();
				if ( uiprops != null )
					txtUIPrjPath.Text = uiprops.ProjectPath;
				else
					txtUIPrjPath.Text = "";
			} else {
				pnlUI.Visible = false;
			}

			ucRepositoryItemListEx1.RepositoryItem = _service;

			this.ResumeLayout(true);
		}
		private void save()
		{
			if ( _service == null )
				_service = new Service();

			_service.Reload();

			_service.Name = txtServiceNm.Text;
			_service.Description = txtServiceDesc.Text;
		}

		private UIComponentProperties getUIProperties()
		{
			UIComponentProperties props = null;
			if ( _service != null ) {
				props = _service.ExtendedProperties.UIComponentProperties;
			}
			return props;
		}
		private void setUIProperties(UIComponentProperties props)
		{
			if ( _service != null && props != null &&
				 _service.ExtendedProperties.UIComponentProperties != props ) {
				_service.ExtendedProperties.UIComponentProperties = props;
			}
		}
		private void openProject()
		{
			UIComponentProperties uiprops = getUIProperties();
			if ( uiprops == null || string.IsNullOrEmpty(uiprops.ProjectPath) ) {
				if ( !selectProject() )
					return;
			}
			//System.Diagnostics.Process p = new System.Diagnostics.Process();
			//p.StartInfo.FileName = uiprops.ProjectPath;
			//p.StartInfo.WorkingDirectory = Path.GetDirectoryName(uiprops.ProjectPath);
			System.Diagnostics.Process.Start(uiprops.ProjectPath);
		}
		private bool selectProject()
		{
			UIComponentProperties uiprops = getUIProperties();
			if ( uiprops == null )
				uiprops = new UIComponentProperties();

			OpenFileDialog dlg = new OpenFileDialog();
			dlg.CheckFileExists = true;
			dlg.Filter =
				"All Project Files (*.sln;*.csproj;*.vbproj;*.vbp;*.exe)|*.sln;*.csproj;*.vbproj;*.vbp;*.exe|" +
				"Solution Files (*.sln)|*.sln|" +
				"C# Project Files (*.csproj)|*.csproj|" +
				"VB Project Files (*.vbproj;*.vbp)|*.vbproj;*.vbp|" +
				"Executable Files (*.exe)|*.exe|" +
				"All files (*.*)|*.*";
			dlg.FilterIndex = 1;
			if ( string.IsNullOrEmpty(uiprops.ProjectPath) )
				dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
			else {
				dlg.InitialDirectory = Path.GetDirectoryName(uiprops.ProjectPath);
				dlg.FileName = uiprops.ProjectPath;
			}
			dlg.Multiselect = false;
			dlg.Title = "Select UI Component Project";
			dlg.DefaultExt = "csproj";
			if ( dlg.ShowDialog() == DialogResult.OK ) {
				txtUIPrjPath.Text = dlg.FileName;
				uiprops.ProjectPath = dlg.FileName;
				setUIProperties(uiprops);
				return true;
			} else {
				return false;
			}
		}
		#endregion

		#region Event Handlers

		private void dataServices_DragEnter(object sender, DragEventArgs e)
		{
		}
		private void dataServices_DragOver(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.None;
			if ( e.Data.GetDataPresent(typeof(TreeNode)) ) {
				TreeNode tn = (TreeNode)e.Data.GetData(typeof(TreeNode));
				if ( tn.Tag is RepositoryItemBase &&
					 ( (RepositoryItemBase)tn.Tag ).RepositoryItemType == ItemType.Component )
					e.Effect = DragDropEffects.Move;
			}
		}
		private void dataServices_DragLeave(object sender, EventArgs e)
		{

		}
		private void dataServices_DragDrop(object sender, DragEventArgs e)
		{
			if ( e.Data.GetDataPresent(typeof(Service)) ) {
				// add service to list
			}
		}

		private void btnUISelectPrj_Click(object sender, EventArgs e)
		{
			selectProject();
		}
		private void btnUIOpenPrj_Click(object sender, EventArgs e)
		{
			openProject();
		}

		#endregion

		private void txtServiceNm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ( e.KeyChar < (char)32 ) return;
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

		#endregion

		protected override void NotifyPropertyChange(string property, object value)
		{
			if ( property == "Name" && value != null ) {
				txtServiceNm.Text = value.ToString();
			}
		}
	}
}
